﻿#include "msgbox.h"

#include <QDebug>
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QKeyEvent>

MsgBox::MsgBox(QString text, QWidget *parent) : QDialog(parent)
{
    QFont font("Helvetica", 12);

    m_lblMsg = new QLabel(this);
    m_lblMsg->setFont(font);
    m_lblMsg->setText(text);

    m_btnOk = new QPushButton("OK", this);
    m_btnOk->setFont(font);
    m_btnOk->setFixedSize(100, 32);
    connect(m_btnOk, &QPushButton::clicked, this, &MsgBox::onOk);

    QHBoxLayout* msgLayout = new QHBoxLayout;
    msgLayout->addWidget(m_lblMsg, 0, Qt::AlignHCenter);

    QHBoxLayout* okLayout = new QHBoxLayout;
    okLayout->addStretch(1);
    okLayout->addWidget(m_btnOk);

    QVBoxLayout* vboxLayout = new QVBoxLayout;
    vboxLayout->addLayout(msgLayout);
    vboxLayout->addLayout(okLayout);

    setLayout(vboxLayout);
    setWindowTitle(QStringLiteral("提示"));
    setFixedSize(320, 200);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint);
}

MsgBox::~MsgBox()
{
}

void MsgBox::setMsg(QString msg)
{
    m_lblMsg->setText(msg);
}

void MsgBox::setButtonText(QString btnText)
{
    m_btnOk->setText(btnText);
}

void MsgBox::keyPressEvent(QKeyEvent *event)
{
    int key = event->key();
    Qt::KeyboardModifiers modifiers = event->modifiers();
    if (modifiers == Qt::ControlModifier || modifiers & Qt::ControlModifier) {
        // Ctrl键
        switch (key) {
        case Qt::Key_B:
            this->accept();
            break;
        default:
            break;
        }

    }

    QDialog::keyPressEvent(event);
}

void MsgBox::onOk()
{
    this->accept();
}


